package fudan.se.project.integration.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonUtils {
    private static final ObjectMapper objectMapper = new ObjectMapper();

    private JsonUtils() {
    }


    public static String toJson(Object object) throws JsonProcessingException {
        return objectMapper.writeValueAsString(object);
    }

    public static String readValue(String jsonStr, String dotPath) throws JsonProcessingException {
        JsonNode jsonNode = objectMapper.readTree(jsonStr);
        final String[] segments = dotPath.split("\\.");
        for (String segment : segments) {
            jsonNode = jsonNode.get(segment);
        }
        return jsonNode.toString();
    }

}
